[TOC]

Function Handle

A function handle is a variable that stores information that uniquely identifies a function. Since a function handle is a variable, it can be stored in a workspace or passed to another function as an input argument. There are totally four types of functions provided by SIMO, namely, built-in functions, main functions, local functions, and anonymous functions. Each of these functions can be represented by one or multiple function handles.

Creating Function Handle

A function handle is created using the @ operator followed by the function name or the function expression. Let's illustrate this with an example. Suppose that you are developing an algorithm that requires the computation of a matrix norm. There are different definitions of matrix norm. You allow users to choose the one that fits their applications. Hence, you create a function with the following declaration, where handle should be associated with a function that computes matrix norms.

output = algo(handle)

The following table shows how function handles can be created for different types of functions. First, for the built-in function (Row 1), it is created by the operator @ followed by the function name norm. In Usage 1 below, the function handle is saved as a variable named handle before entering algo as an input argument. This can be useful if you want to access the same function handle later. Otherwise, you may use the syntax in Usage 2, which makes the code shorter and sometimes more readable.

Instead of using the built-in function, a user may want to provide his or her own function to compute the matrix norm. In this situation, the function handle should refer to a main, local or anonymous function defined by the user. The usages for main and local functions can be seen from Rows 2 and 3 of the table, respectively. For an anonymous function, since it has no function name (that's why anonymous), the @ operator should be followed by its expression. In this example, the norm is given by the largest element in the matrix x. Hence, the function handle is defined by @(x) max(max(x)).

 TypeFunction nameUsage 1Usage 2
1Built-in functionnormhandle = @norm
algo(handle)
algo(@norm)
2Main functionmy_normhandle = @my_norm
algo(handle)
algo(@my_norm)
3Local functionmy_local_normhandle = @my_local_norm
algo(handle)
algo(@my_local_norm)
4Anonymous functionAnonymous function has no namehandle = @(x) max(max(x))
algo(handle)
algo(@(x)max(max(x)))

Calling Function Handle

When a function handle, say, handle, is called without input arguments, we use handle(). We need to use two parentheses, since handle (without parentheses) means the variable storing the function handle, whereas handle() means calling the function pointed by the function handle.

If a function expects input arguments, we call its function handle with input arguments between the parentheses, such as, handle(in1, in2, in3). If the number of input arguments provided is incorrect, a warning or an error is thrown.

Scope

When a function handle is created, it is stored as a variable in the current workspace. It is only accessible from within the current workspace. This variable behaves in the exact same way as variables storing other data types. This is summarised below:

Path

A function handle does not store the path of a user-defined function (main or local). Hence, it will fail if a function handle is used to execute a user-defined function not located in the work folder. Built-in functions have no such limitation since they are globally accessible.

For example, we define the function myfunc(b) in the Home folder:

function a = myfunc(b)
a = b
end

The current work folder is set to Home. We run the following from the console, and it runs successfully.

Input
handle = @myfunc
handle(1)
Output
handle = (main function)
 myfunc

a = 
 1.0000000

ans = 
 1.0000000

However, when we have changed the work folder to Home/Debug, calling the same statements again from the console fails, as shown in the following screenshot. 💡 MATLAB does not have this limitation.

func_handle_path